package com.atguigu.bigdata.chapter07.state;

import org.apache.flink.api.common.state.BroadcastState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReadOnlyBroadcastState;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.BroadcastConnectedStream;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author lzc
 * @Date 2022/9/6 15:03
 */
public class Flink03_Operator_BroadCastState {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        // 每隔3秒中做一次checkpoint
        // 这样理解: 每隔3秒,把程序的所有的状态做一次持久化, 程序出了问题重启的时候, 可以从持久化状态中恢复数据
        env.enableCheckpointing(3000);
        
        
        // 1. 读取一个数据流
        DataStreamSource<String> dataStream = env.socketTextStream("hadoop162", 8888);
        // 2. 读取一个配置流
        DataStreamSource<String> confStream = env.socketTextStream("hadoop162", 9999);
        // 3. 把配置流做成一个广播流
        MapStateDescriptor<String, String> bcStateDesc = new MapStateDescriptor<>("bcState", String.class, String.class);
        BroadcastStream<String> bcStream = confStream
            .broadcast(bcStateDesc);
        // 4. 数据流去 connect 广播流
        BroadcastConnectedStream<String, String> dataConfStream = dataStream.connect(bcStream);
        // 5. 分布处理广播流中的数据和数据流中的数据
        dataConfStream
            .process(new BroadcastProcessFunction<String, String, String>() {
    
                // 5.2 处理数据流, 从广播状态读取配置信息, 来根据配置信息决定代码的处理方式
                
                // 数据流中的数据, 每来一条执行一次
                @Override
                public void processElement(String data, ReadOnlyContext ctx, Collector<String> out) throws Exception {
                    System.out.println("Flink03_Operator_BroadCastState.processElement");
                    ReadOnlyBroadcastState<String, String> state = ctx.getBroadcastState(bcStateDesc);
    
                    String aSwitch = state.get("switch");
                    if ("1".equals(aSwitch)) {
                        out.collect("使用 1 号逻辑");
                    }else if ("2".equals(aSwitch)) {
                        out.collect("使用 2 号逻辑");
                    }else{
                        
                        out.collect("使用 默认号 号逻辑");
                    }
                }
    
                // 5.1 广播流中的数据存入到广播状态, 状态会自动广播到每个并行度中
                
                // 配置流中的数据每来一条, 每个并行度执行一次
                @Override
                public void processBroadcastElement(String value, Context ctx, Collector<String> out) throws Exception {
                    System.out.println("Flink03_Operator_BroadCastState.processBroadcastElement");
                    // 获取广播状态
                    BroadcastState<String, String> state = ctx.getBroadcastState(bcStateDesc);
                    state.put("switch", value);
                    
                }
            })
            .print();
       
        
        
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
}
